<template>
  <div>
    <div class="data-list">
      <div class="left">
        <span
          ><el-icon> <Tickets /> </el-icon
        ></span>
        <span>数据列表</span>
      </div>
      <div>
        <button
          v-for="item in datalisttitle"
          :key="item.val"
          @click="changeShow"
        >
          {{ item.val }}
        </button>
      </div>
    </div>
    <!-- 筛选弹框 -->
    <el-dialog v-model="showSelectModule" title="选择商品" width="30%">
      <el-table
        :data="tableDataSelect.list"
        @selection-change="Functi"
        style="width: 100%"
        :border="true"
      >
        <el-table-column type="selection" width="80" />
        <el-table-column prop="name" label="商品名称" width="170" />
        <el-table-column prop="name" label="货号" width="170" />
        <el-table-column prop="name" label="价格" width="170" />
      </el-table>
      <!-- 分页器 -->
      <template #footer>
        <span class="dialog-footer">
          <div class="foot_box">
            <el-pagination
              v-model:current-page="tableDataSelect.pageNum"
              v-model:page-size="tableDataSelect.pageSize"
              background
              layout="prev, pager, next"
              :total="tableDataSelect.total"
              @size-change="handleSizeChangeSelect"
              @current-change="handleCurrentChangeSelect"
            />
          </div>
        </span>
        <span>
          <el-button @click="cancelSelectDataFun">取消</el-button>
          <el-button type="primary" @click="addSelectDataFun">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import * as api from "../../request/api";
import { defineProps } from "vue";

let showSelectModule = ref(false);
let changeShow = () => {
  showSelectModule.value = true;
};
defineProps<{
  datalisttitle: Array<{
    val: string;
  }>;
}>();
// 筛选品牌
let tableDataSelect = reactive({
  list: <any>[],
  pageNum: 1,
  pageSize: 5,
  total: 28,
  totalPage: 6,
  nickName: "",
});
const getShopData = () => {
  showSelectModule.value = true;
  getShopDataFun();
};
const getShopDataFun = () => {
  api.homebrand
    .brandListApi({
      pageNum: tableDataSelect.pageNum,
      pageSize: tableDataSelect.pageSize,
    })
    .then((res) => {
      tableDataSelect.list = res.list;
      tableDataSelect.total = res.total;
    });
};
const handleSizeChangeSelect = (val: number) => {
  tableDataSelect.pageSize = val;
  getShopDataFun();
};
const handleCurrentChangeSelect = (val: number) => {
  tableDataSelect.pageNum = val;
  getShopDataFun();
};
// 添加筛选的数据
let addSelectData = reactive([]);
let Functi = (val: any) => {
  addSelectData = val.map((item: any) => {
    console.log(item.id);
    return { id: item.id, name: item.name };
  });
  console.log(addSelectData);
};
let addSelectDataFun = () => {
  api.homebrand.homeBrandCreate(addSelectData);
  showSelectModule.value = false;
};
let cancelSelectDataFun = () => {
  showSelectModule.value = false;
};
// 搜索请求
// 定义搜索数据
let nickname = ref("");

// 列表数据
let tableData = reactive({
  list: <any>[],
  pageNum: 1,
  pageSize: 5,
  total: 28,
  totalPage: 6,
  nickName: "",
});

// 获取所有商品数据的请求
let getProductList = () => {
  api.homebrand
    .homeBrandApi({
      pageNum: tableData.pageNum,
      pageSize: tableData.pageSize,
      brandName: nickname.value,
    })
    .then((res) => {
      console.log(res);
      tableData.list = res.list;
      tableData.total = res.total;
    });
};
getProductList();

let handleSizeChange = (val: number) => {
  // console.log(val);
  tableData.pageSize = val;
  getProductList();
};
// 改变当前页数
let handleCurrentChange = (val: number) => {
  // console.log(val);
  tableData.pageNum = val;
  getProductList();
};
</script>

<style lang="scss" scoped>
.data-list {
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 1px solid #ccc;
  box-shadow: 1px 2px 3px #ccc;
  background-color: #fff;
  margin-bottom: 20px;

  div {
    button {
      padding: 6px 10px;
      margin: 0 10px;
      color: #666;
      background-color: #fff;
      border: 1px solid #ccc;
      font-size: 12px;
      cursor: pointer;
    }
  }

  .left {
    display: flex;
    align-items: center;
  }
}
</style>
